package com.translate.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.CustomerFollowPersonMapper;
import com.translate.entity.CustomerFollowPerson;
import com.translate.web.service.ICustomerFollowPersonService;
import com.basic.service.impl.BaseServiceImpl;


@Service
public class CustomerFollowPersonServiceImpl extends BaseServiceImpl<CustomerFollowPerson> implements ICustomerFollowPersonService {

	public CustomerFollowPersonServiceImpl(CustomerFollowPersonMapper mapper) {
		super(mapper);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int addFollowPerson(int customerId, long followId) {
		
		CustomerFollowPerson customerFollowPerson = new CustomerFollowPerson();
		
		customerFollowPerson.setCustomerId(customerId);
		
		customerFollowPerson.setFollowMe((int)followId);
		
		return this.insert(customerFollowPerson);
	}

	@Override
	public <S> List<CustomerFollowPerson> getCustomerFollowPersons(List<S> list, Function<S, Integer> function) {
		
		Integer[] integers = list.stream().map(e -> function.apply(e) ).toArray(Integer[]::new);
		
		return null;
	}

	@Override
	public Integer[] getFollowPerson(int customerId) {
		return customerFollowPersonsToCache(customerId).stream().map(e->e.getFollowMe()).toArray(Integer[]::new);
	}

	
	
	@Override
	protected Class<CustomerFollowPerson> getEntityClass() {
		// TODO Auto-generated method stub
		return CustomerFollowPerson.class;
	}

	/**
	 * 获取跟进人id
	 * @param customerId 客户编号
	 * @return
	 */
	protected List<CustomerFollowPerson> customerFollowPersonsToCache(int customerId) {
		
		String key = RedisConstant.getKeyByCustomerFollowID(customerId);
		
		ValueOperations<String, List<CustomerFollowPerson>> valueOperations = this.redisTemplate.opsForValue();
		
		List<CustomerFollowPerson> customerFollowPersons = valueOperations.get(key);
		
		if (null != customerFollowPersons) {
			return customerFollowPersons;
		}
		
		customerFollowPersons = this.getDataByIds(customerId);
		
		if (null == customerFollowPersons) {
			customerFollowPersons = new ArrayList<>();
			
			valueOperations.set(key, customerFollowPersons, 3, TimeUnit.SECONDS);
			
			return customerFollowPersons;
		}
		
		valueOperations.set(key, customerFollowPersons, 6, TimeUnit.SECONDS);

		return customerFollowPersons;
	}
	
	
	private RedisTemplate<String, List<CustomerFollowPerson>> redisTemplate;


	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<CustomerFollowPerson>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}



//	@Override
//	public <P> int getInsertIdByData(ICustomerParam<P> customerParam) {
//		
//		prepareData(customerParam);
//		
//		return this.parseInsert(customerParam, Customer.class);
//	}
//	
//	protected <P> void prepareData(ICustomerParam<P> customerParam) {
//		
//		if (null == customerParam.getEmail()) {
//			customerParam.setEmail("");
//		} 
//		
//		if (null == customerParam.getTelephone()) {
//			customerParam.setTelephone("");
//		}
//		
//	}
//	
//	protected int getId(Customer data) {
//		return data.getId();
//	}
//
//	@Override
//	public <P> int getUpdateByData(ICustomerParam<P> customerParam) {
//		prepareData(customerParam)
//		;
//		
//		return this.parseUpdate(customerParam, Customer.class);
//	}

}
